<?php

namespace app\api\controller;

use addons\ask\model\Comment;
use app\admin\model\user\UserExperts;
use app\admin\model\user\UserExpertsQualification;
use app\common\controller\Api;
use app\common\library\ali\rabittmq\Producer;
use app\common\model\Category;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        //获取分类列表
        $category = new Category();
        $categorylist = collection($category->where('pid', 0)->field('type,id,name,flag,image')->select())->toArray();
        $ue = new UserExperts();
        $categoryExpertsList = [];
        foreach ($categorylist as $key => $value) {
            $categoryExpertsList[$key]['name'] = $value['name'];
            $categoryExpertsList[$key]['category_id'] = $value['id'];
            $categoryExpertsList[$key]['experts'] = $ue->cIdExpertsList(['c_id' => $value['id']], '', '1', 3);
        }
        $data = [
            'categorylist' => $categorylist,
            'categoryExpertsList' => $categoryExpertsList
        ];

        $this->success('请求成功', $data, 200);
    }

    /**
     *分类列表
     * */

    public function category()
    {
        $pid = $this->request->param('pid', 0);
        $category = new Category();
        $categorylist = collection($category->where('pid', $pid)->field('type,id,name,flag,image')->select())->toArray();
        $this->success('成功', $categorylist, 200);
    }

    /**
     * 专家列表
     * */
    public function category_list()
    {
        list($where, $order, $page) = $this->where();
        $ue = new UserExperts();

        $list = $ue->cIdExpertsList($where, $order, $page);
        $this->success('成功', $list, 200);
    }

    public function where()
    {
        $params = $this->request->param();
        $where = (array)[];
        $order = 'e.u_uid desc';
        $consulting = 'sound_price'; //咨询方式 默认 音频
        if (!isset($params['consulting']) || empty(trim($consulting))) {
            $consulting = $consulting;
        } else {
            $consulting = $params['consulting'];
        }

        if ($params && is_array($params)) {

            $c_id = isset($params['c_id']) ? $params['c_id'] : 0;
            if ($c_id) {                   //分类
                $where = array_merge((array)$where, (array)['c_id' => $c_id]);
            }
            $price = isset($params['price']) ? $params['price'] : 0;

            if ($price) {
                //价格区间
                $price_max = (int)isset($params['price_max']) ? (int)$params['price_max'] : 0;
                $price_min = (int)isset($params['price_min']) ? (int)$params['price_min'] : 0;

                if ($price_min && $price_max) {
                    $where = array_merge((array)$where, (array)['ep.' . $consulting => ['between', "$price_min , $price_max"]]);
//                        $where[ $consulting] = ['between' => "$price_min , $price_max"];
                } elseif ($price_max == 0 && $price_min > 0) {
                    $where = array_merge((array)$where, (array)['ep.' . $consulting => ['>', $price_min]]);
                } elseif ($price_max > 0 && $price_min == 0) {
                    $where = array_merge((array)$where, (array)['ep.' . $consulting => ['<', $price_max]]);
                }
            }
            $sex = isset($params['sex']) ? $params['sex'] : 0;
            if ($sex) {
                $where = array_merge($where, ['u.gender' => $sex]);
            }
//                var_dump($where);die;
            $order_count = isset($params['order_count']) ? $params['order_count'] : 0;
            if ($order_count) {
                $order = 'e.serve_count desc';
            }
            $sound_price = isset($params['order_price']) ? $params['order_price'] : 0;
            if ($sound_price) {
                $order = 'ep.sound_price asc';
            }
            $newest = isset($params['order_newest']) ? $params['order_newest'] : 0;
            if ($newest) {
                $order = 'e.u_create_time desc';
            }
            $page = isset($params['page']) ? $params['page'] : 1;


            return [
                $where,
                $order,
                $page
            ];
        } else {
            $page = 1;
            return [
                $where,
                $order,
                $page
            ];
        }
    }

    /**
     * 专家详情
     * */
    public function experts_detail()
    {
        $uid = $this->request->param('experts_id');
        $ue = new UserExperts();
        $info = $ue->detail($uid);
        $this->success('成功', $info);
    }

    //测试
    public function test1()
    {
        $p = new Producer();
        $p->setQueue('yanci');
        for ($i = 1; $i <= 1000; $i++) {
            $msg = json_encode(['order_id' => $i]);
            $p->setMessage($msg, 'delayed', 10000);
            $p->basic_publish();
            var_dump($i);
        }

    }

    public function test2()
    {

        $str = file_get_contents(ROOT_PATH . 'test1.txt');
        $arr = explode("\r\n", trim($str));
        sort($arr);
        echo "<pre/>";
        print_r($arr);
    }

    /**
     * 专家评论列表
     * */
    public function experts_comment_list()
    {
        $experts_id = $this->request->param('experts_id', 0);
        $page = $this->request->param('page', 1);
        $page_count = $this->request->param('page_count', 10);
        $page = ($page * $page_count) - $page_count;
        $comment = new Comment();
        $comment_list = $comment->alias('c')
            ->join('fa_user u', 'u.id = c.user_id')
            ->where(['c.experts_id' => $experts_id, 'c.status' => 'normal', 'c.type' => 'order'])
            ->field('c.content,c.voteup,u.nickname,u.avatar,c.createtime')
            ->order('createtime desc')
            ->limit($page, $page_count)
            ->select();
        if ($comment_list) {
            $comment_list = collection($comment_list)->toArray();

            foreach ($comment_list as $k => $v) {
                $comment_list[$k]['createtime'] = date('Y-m-d', $v['createtime']);
            }
        }
        $this->success('请求成功', $comment_list, 200);
    }

    /**
     * 首页用户心声
     * */
    public function aspirations()
    {
        $page = $this->request->param('page', 1);
        $page_count = $this->request->param('page_count', 10);
        $ofset = ($page * $page_count) - $page_count;
        $comment = new Comment();
        $comment_list = $comment->alias('c')
            ->join('fa_user u','u.id = c.user_id')
            ->join('fa_user e','e.id = c.experts_id')
            ->join('fa_user_experts ue','ue.u_uid = e.id')
            ->field('c.content,c.createtime,e.id expert_id ,u.nickname user_nickname ,u.avatar user_avatar,e.avatar experts_avatar,e.nickname experts_nickname,c.voteup,ue.serve_count')
            ->where('c.status','normal')
            ->limit($ofset,$page_count)
            ->order('c.createtime desc')
            ->select();
        $ueq = new UserExpertsQualification();

        if($comment_list){
            $comment_list = collection($comment_list)->toArray();
            foreach ($comment_list as $k=>$v){
                //证书
                $z_list = $ueq->where('uid', $v['expert_id'])->field('name')->select();
                $z_str = '';
                if ($z_list) {
                    foreach ($z_list as $z_k => $z_v) {
                        if ($z_k < 2) {
                            $z_str .= ' ' . $z_v['name'];
                        }
                    }
                }
                $comment_list[$k]['user_qualification'] = $z_str;
            }
        }
        $this->success('请求成功',$comment_list,200);
    }

}
